//
//  BottomFloatView.swift
//  ZZWeChatFloatView
//
//  Created by 周晓瑞 on 2018/6/13.
//  Copyright © 2018年 apple. All rights reserved.
//

import UIKit

enum BottomFloatViewType {
    case black //黑色，默认
    case red // 取消即红色
}

class BottomFloatView: UIView {
    
    var type : BottomFloatViewType = BottomFloatViewType.black{
        didSet{
            if type == BottomFloatViewType.red {
                backgroundColor = UIColor.red
                tipsLab.text = "浮窗"
                setNeedsDisplay()
            }
        }
    }
    var insideBottomSeleted : Bool = false {
        didSet{
            if type == BottomFloatViewType.red {
                tipsLab.text =  insideBottomSeleted ? "取消弹窗" : "浮窗"
                self.imageView.image = UIImage(named: insideBottomSeleted ? "demo_cancel_float":"demo_cancel_float_default")
            }
            
            setNeedsDisplay()
        }
    }
    fileprivate lazy var tipsLab: UILabel = {
        return UILabel()
    }()
    fileprivate lazy var maskLayer1: CAShapeLayer = {
        let layer1 = CAShapeLayer()
        return layer1
    }()
    fileprivate lazy var imageView: UIImageView = {
        return UIImageView()
    }()
    
   override init(frame: CGRect) {
        super.init(frame: frame)
        setUp()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUp() {
        backgroundColor = UIColor(white: 0.0, alpha: 0.65)
        layer.mask = maskLayer1
        
        ///imageView
        self.imageView.image = UIImage.init(named: "demo_cancel_float_default")
        self.imageView.contentMode = .center
        self.addSubview(self.imageView)
        
        ///浮窗
        tipsLab.font = UIFont.systemFont(ofSize: 15)
        tipsLab.textColor = UIColor.white
        tipsLab.textAlignment = .center
        tipsLab.text = "浮窗"
        addSubview(tipsLab)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        ///
        let ima_w: CGFloat = 40
        let ima_h: CGFloat = 40
        let ima_x: CGFloat = self.frame.size.width/2.0+20
        let ima_y: CGFloat = self.frame.size.height/2.0-10
        let frame_ima = CGRect(x: ima_x, y: ima_y, width: ima_w, height: ima_h)
        self.imageView.frame = frame_ima
        
        ///
        let lab_h: CGFloat = 20
        let lab_x: CGFloat = 2
        let lab_w: CGFloat = 80
        let lab_y: CGFloat = self.imageView.frame.maxY+8
        let frame_lab = CGRect(x: lab_x, y: lab_y, width: lab_w, height: lab_h)
        self.tipsLab.frame = frame_lab
        
        let ima_cen_x = self.imageView.center.x
        var lab_point = self.tipsLab.center
        lab_point.x = ima_cen_x
        self.tipsLab.center = lab_point
    }
    
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        let scale : CGFloat = insideBottomSeleted ? 1.0 : 0.8
        let bezierPath = UIBezierPath(arcCenter: CGPoint(x: bounds.width, y: bounds.height), radius: bounds.width * scale, startAngle:CGFloat(Double.pi), endAngle: CGFloat(1.5 * Double.pi), clockwise: true)
        bezierPath.addLine(to: CGPoint(x: bounds.width, y: bounds.height))
        bezierPath.close()
        maskLayer1.path = bezierPath.cgPath
        
        /*if type == BottomFloatViewType.red {
           let circleB1 = UIBezierPath(arcCenter: CGPoint(x: bounds.width * 0.7, y: bounds.height * 0.7), radius: 10, startAngle: 0, endAngle: CGFloat(Double.pi * 2.0), clockwise: true)
           circleB1.lineWidth = 3.0
            UIColor.white.setStroke()
           circleB1.stroke()
        }*/
    }
}
